package com.ymatou.iapi.productlist.testcase;

import java.util.Arrays;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productlist.service.GetTopProductListByLiveIdCall;
import com.ymatou.iapi.sellerproduct.service.SetRecommendCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_GetTopProductListByLiveId {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据直播Id,查询置顶商品id list");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetTopProductListByLiveIdCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void GetTopProductListByLiveId() {
		Logger.start(true, "根据直播Id,查询置顶商品id list");
		try{
			String productId1 = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId2 = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productId3 = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");

			int liveId = LiveCallService.createLiveInProgress(sellerName,sellerId).getJSONObject("Data").getInt("ActivityId");
			
			SellerProductCallService.addProductInLive(sellerId, liveId, productId1);
			SellerProductCallService.addProductInLive(sellerId, liveId, productId2);
			SellerProductCallService.addProductInLive(sellerId, liveId, productId3);

			new SetRecommendCall().callAndGetReturnData(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId,"ProductId", productId1));
			new SetRecommendCall().callAndGetReturnData(MapUtil.hashMap("UserId", sellerId, "ActivityId", liveId,"ProductId", productId2));
																		
			JSONObject retBean = GetTopProductListByLiveIdCall.GetTopProductListByLiveId(liveId);
			Logger.verifyEquals(200,retBean.getInt("Code"),  "响应报文-返回码");
			
			JSONArray productList = retBean.getJSONObject("Data").getJSONArray("ProductList");
			Logger.verifyEquals(2,productList.length(),  "响应报文-productList");
			
			for (int i=0;i<productList.length();i++){
				String productId = JsonBeanHelper.getJsonBeanNodeObj(productList.getJSONObject(i), "Product->ProductId").toString();
				Logger.verifyEquals(true, Arrays.asList(productId1,productId2).contains(productId), "响应报文-productList包含置顶商品");
			}
			
			for (int i=0;i<productList.length();i++){
				String productId = JsonBeanHelper.getJsonBeanNodeObj(productList.getJSONObject(i), "Product->ProductId").toString();
				Logger.verifyEquals(false, Arrays.asList(productId3).contains(productId), "响应报文-productList不包含未置顶商品");
			}
			 
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}
